package com.ljy.my_study.tomcat原理.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketImplFactory;

/** 
* @author James
* @date 2018年6月5日 
*/
public class Server {
	/**  
     * 容器主方法  
     * @param args  
     * @throws InstantiationException   
     * @throws ClassNotFoundException   
     * @throws SecurityException   
     * @throws NoSuchMethodException   
     */  
    public static void main(String[] args) {  
        System.out.println("little_tomcat_is_running!");  
        try {  
            ServerSocket serverSocket = new ServerSocket(80);  
            while (true) {  
                //服务器每接受一次请求，创建一个socket对象  
                Socket socket = serverSocket.accept(); 
                try {
                	BufferedReader bReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
                	ObjectOutputStream out=new ObjectOutputStream(socket.getOutputStream());
                    String line = bReader.readLine();  
                    StringBuilder sb=new StringBuilder();
                    sb.append(line);
                    String nextLine=null;
                    System.out.println(line);
                    
//                    while((nextLine=bReader.readLine())!=null) {
                    while(bReader.ready()) {
                    	sb.append("\n");
                    	sb.append(nextLine);
                    	System.out.println(nextLine);
                    }
//                    System.out.println("in:\n"+sb.toString());
                    if (!(null==line)) {  
                        ClintRequestBean requestBean = new ClintRequestBean(line);  
                        System.out.println("客户端请求："+requestBean.toReadString());  
                        System.out.println("请求参数[路径]："+requestBean.getRequestParm().get("path"));  
                        System.out.println("请求参数[参数表]："+requestBean.getRequestParm().get("attrs"));  
                        ClassLoader classLoader = ClassLoader.getSystemClassLoader();  
                        try {  
                            classLoader.loadClass("com.ljy.my_study.tomcat原理.server.ExClass");  
                            System.out.println("动态加载ExClass类--成功");  
                        } catch (ClassNotFoundException e) {  
                            e.printStackTrace();  
                            System.out.println("动态加载ExClass类--失败");  
                        }  
                        Class<?> exClass = null;  
                        try {  
                            exClass = Class.forName("com.ljy.my_study.tomcat原理.server.ExClass"
                            		+ "");  
                            System.out.println("动态初始化ExClass类--成功");  
                        } catch (ClassNotFoundException e) {  
                            e.printStackTrace();  
                            System.out.println("动态初始化ExClass类--失败");  
                        }  
                        Method method;  
                        try {  
                            method = exClass.getMethod("test", null);  
                            System.out.println("得到ExClass对象的"+method.getName()+"方法");  
                            try {  
                                System.out.println("执行ExClass对象的"+method.getName()+"方法");  
                                try {
    								Object resultObj=method.invoke(exClass.newInstance(), null);
    								if(resultObj==null) {
										out.writeObject("");
    								}
    								out.writeObject("<p>"+resultObj.toString()+"</p>");
    							} catch (InstantiationException e) {
    								// TODO Auto-generated catch block
    								e.printStackTrace();
    							}  
                            } catch (IllegalAccessException e) {  
                                // TODO Auto-generated catch block  
                                e.printStackTrace();  
                            } catch (IllegalArgumentException e) {  
                                // TODO Auto-generated catch block  
                                e.printStackTrace();  
                            } catch (InvocationTargetException e) {  
                                // TODO Auto-generated catch block  
                                e.printStackTrace();  
                            }  
                        } catch (NoSuchMethodException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        } catch (SecurityException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                    }  
                    out.close();
                    bReader.close();  
                    socket.close();  
                }catch(Exception e) {
                	e.printStackTrace();
                }
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
          
    }  
}
